home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / pc / ps40sdk / examples / common / sources.c / fileutilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-24  |  5.1 KB  |  201 lines

  1. /*
  2.     File: FileUtilities.c
  3.  
  4.     Copyright (c) 1996, Adobe Systems Incorporated.
  5.     All rights reserved.
  6.  
  7.     C source file for file utilities.
  8. */
  9.  
  10. #include "FileUtilities.h"
  11.  
  12. /*****************************************************************************/
  13.  
  14. Boolean PISetSaveDirectory (Handle alias, Str255 s, 
  15.                                   Boolean query, short *rVRefNum)
  16. {
  17.     
  18.     CInfoPBRec            pb;
  19.     Boolean                wasChanged;
  20.     FSSpec                spec;
  21.     int32                gotErr = 0;
  22.     
  23.     if (alias != NULL)
  24.     {
  25.     
  26.         ResolveAlias(nil, (AliasHandle) alias, &spec, &wasChanged);
  27.         
  28.         /* set up file param structure so we can get whether we've been handed
  29.            a file or a directory */
  30.         
  31.         pb.dirInfo.ioCompletion = nil;
  32.         pb.dirInfo.ioNamePtr = spec.name;
  33.         pb.dirInfo.ioVRefNum = spec.vRefNum;
  34.         pb.dirInfo.ioFRefNum = 0;
  35.         pb.dirInfo.ioFDirIndex = 0;
  36.         pb.dirInfo.ioDrDirID = spec.parID;
  37.         
  38.         gotErr = PBGetCatInfoSync( &pb );
  39.         
  40.         /* now check for NO error and this is NOT a directory */
  41.         // nah, don't need to check for error.
  42.         /* even if we got an error, these fields will be valid (enough) */
  43.         
  44.         if (spec.name[0] > 0)
  45.         /* the scripting system passed us something.  Figure out what to do with it. */
  46.         {
  47.             if (pb.dirInfo.ioFlAttrib & ioDirMask)
  48.             { // the scripting system passed us a directory.  Add filename to it
  49.                // if we're not popping a dialog.
  50.                                     
  51.                 if (!query)
  52.                 { // if we were going to show this, we could not
  53.                   // make it :dir:name since that would be bad for the file dialog
  54.                     spec.name[++spec.name[0]] = ':';
  55.                     PICopy(&spec.name[ (spec.name[0]+1) ], &s[1], s[0]);
  56.                     spec.name[0] += s[0];
  57.                     s[0] = spec.name[0]+1;
  58.                     s[1] = ':'; // since we're a directory
  59.                     PICopy (&s[2], &spec.name[1], spec.name[0]); // copy name to output string
  60.                 }
  61.                 else
  62.                 { // querying so set dir
  63.                     LMSetSFSaveDisk(-pb.dirInfo.ioVRefNum);
  64.                     LMSetCurDirStore(pb.dirInfo.ioDrDirID);
  65.                 }
  66.             }
  67.             else
  68.             {
  69.                 /* we were given a full pathname. Copy the name. */
  70.                 PICopy (s, spec.name, (spec.name[0])+1);
  71.                 
  72.                 if (query)
  73.                 { // we're showing our dialog, set set directory from alias
  74.                     LMSetSFSaveDisk(-spec.vRefNum);
  75.                     LMSetCurDirStore(spec.parID);
  76.                 }
  77.             }
  78.             /* otherwise we'll just use whatever the export parameter block
  79.                has passed us */
  80.         }
  81.  
  82.         /* set gStuff->vRefNum as working directory (alias) from vRefNum
  83.            from hard vRefNum/parID */
  84.         PICloseAndOpenWD(spec.vRefNum, spec.parID, rVRefNum);
  85.         return TRUE;
  86.     }
  87.     else return FALSE; // aliashandle was null
  88. }
  89.  
  90. /*****************************************************************************/
  91.  
  92. Boolean PICreateFile (Str255 filename, short vRefNum, const ResType creator, 
  93.                       const ResType type, FileHandle *fRefNum, short *result)
  94. {
  95.  
  96.     /* Mark the file as not being open. */
  97.     
  98.     *fRefNum = 0;
  99.     
  100.     /* Create the file. */
  101.  
  102.     (void) FSDelete (filename, vRefNum);
  103.  
  104.     if (!TestAndStoreResult (result, Create (filename, vRefNum, creator, type)))
  105.         return FALSE;
  106.         
  107.     /* Open the file. */
  108.  
  109.     if (!TestAndStoreResult (result, FSOpen (filename, vRefNum, fRefNum)))
  110.     {
  111.         /* failure */
  112.         (void) FSDelete (filename, vRefNum);
  113.         return FALSE;
  114.     }
  115.     else
  116.         return TRUE; // succeeded
  117.         
  118. }
  119.  
  120. /*****************************************************************************/
  121.  
  122. Boolean PICloseFile (Str255 filename, short vRefNum,
  123.                      FileHandle fRefNum, Boolean sameNames,
  124.                      Boolean *dirty, AliasHandle *alias, short *result)
  125. {
  126.  
  127.     FSSpec            spec;
  128.     OSErr            gotErr;
  129.     
  130.     // Close the file if it is open.
  131.     if (fRefNum != 0)
  132.         (void) TestAndStoreResult (result, FSClose (fRefNum));
  133.         
  134.     // Delete the file if we had an error.    
  135.     if (*result != noErr)
  136.         (void) FSDelete (filename, vRefNum);
  137.             
  138.     // Flush the volume anyway.
  139.     if (!TestAndStoreResult (result, FlushVol (NULL, vRefNum))) return FALSE;
  140.     
  141.     // Mark the file as clean.
  142.     *dirty = FALSE;
  143.     
  144.     /* create alias for scripting system */
  145.     
  146.     /* first, make FSSpec record */
  147.     FSMakeFSSpec(vRefNum, 0, filename, &spec);
  148.     
  149.     if (sameNames)
  150.     { // didn't enter new filename, so set filename to nothing
  151.         spec.name[ (spec.name[0] = 0)+1 ] = 0;
  152.     } // otherwise use filename and store entire reference
  153.  
  154.     gotErr = NewAlias(nil, &spec, alias);
  155.  
  156.     return (gotErr == noErr);
  157.     
  158. }
  159.     
  160. /*****************************************************************************/
  161.  
  162. Boolean TestAndStoreResult (short *res, OSErr result)
  163. {
  164.     if (result != noErr)
  165.         {
  166.         if (*res == noErr)
  167.             *res = result;
  168.         return FALSE;
  169.         }
  170.     else
  171.         return TRUE;    
  172. }
  173.     
  174. /*****************************************************************************/
  175.  
  176. Boolean TestAndStoreCancel (short *res, Boolean tocancel)
  177. {    
  178.     if (tocancel)
  179.         {
  180.         if (*res == noErr)
  181.             *res = userCanceledErr;
  182.         return FALSE;
  183.         }
  184.     else
  185.         return TRUE;    
  186. }
  187.  
  188. /*****************************************************************************/
  189. // Creates new working directory and deletes old one if it was valid
  190.  
  191. void PICloseAndOpenWD(const short vRefNum, const long dirID, short *rVRefNum)
  192. {
  193.     short oldVRefNum = *rVRefNum;
  194.     
  195.     OpenWD(vRefNum, dirID, 0, rVRefNum);
  196.     if (*rVRefNum != oldVRefNum && oldVRefNum < -100)
  197.     { // small negative numbers are actual volumes (-1, etc.)
  198.       // large negative numbers (-32766, etc.) are WD's to be closed
  199.         CloseWD(oldVRefNum); // close our old vRefNum
  200.     }
  201. }